package com.wushijia.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class TcpTest {

  public static void main(String[] args) throws IOException {
    String hostName = "www.baidu.com";
    int port = 80;
    String filename = "/index.html";
    try (Socket socket = new Socket(hostName, port); BufferedReader from = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintWriter to = new PrintWriter(
            new OutputStreamWriter(socket.getOutputStream()));) {
      to.println("GET " + filename + "HTTP/1.1\r\nHost: " + hostName + "\r\n\r\n");
      to.flush();
      for (String l = null; (l = from.readLine()) != null;) {
        System.out.println(l);
      }
    }
  }
}
